package cn.lanqiao.common.interceptor;


import cn.lanqiao.common.utils.TokenUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JwtInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)  {

        String authHeader = request.getHeader("Authorization");
        if (authHeader == null || !authHeader.startsWith("Bearer ")) {
            response.setStatus(200);
            return false;
        }

        //取得token
        String token = authHeader.substring(7);
        //验证token
        boolean verify = TokenUtil.verify(token);
        if(!verify){
            request.setAttribute("username","token验证失败");
            return false;
        }

        return true;

    }

}